// JsonRpc框架-rpc功能联调
#include "../../common/dispatcher.hpp"
#include "../../server/rpc_router.hpp"


void Add(const Json::Value &req, Json::Value &rsp) {
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
}

int main()
{
    auto router = std::make_shared<rcrpc::server::RpcRouter>();
    std::unique_ptr<rcrpc::server::SDescribeFactory> desc_factory(new rcrpc::server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1",rcrpc::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2",rcrpc::server::VType::INTEGRAL);
    desc_factory->setReturnType(rcrpc::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    router->registerMethod(desc_factory->build());

    auto cb = std::bind(&rcrpc::server::RpcRouter::onRpcRequest, router.get(),
    std::placeholders::_1,std::placeholders::_2);

    auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
    dispatcher->registerHandler<rcrpc::RpcRequest>(rcrpc::MType::REQ_RPC, cb);

    auto server = rcrpc::ServerFactory::create(9090);
    auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
        std::placeholders::_1, std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}


// dispatcher测试
// #include "../../common/dispatcher.hpp"
// // 客户端服务器通信测试
// #include "../../common/message.hpp"
// #include "../../common/net.hpp"

// void onRpcResquest(const rcrpc::BaseConnection::ptr& conn,rcrpc::RpcRequest::ptr& msg)
// {
//     std::cout << "收到了Rpc请求:" << msg->method() << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_req = rcrpc::MessageFactory::create<rcrpc::RpcResponse>();
//     rpc_req->setId("11111");
//     rpc_req->setMType(rcrpc::MType::RSP_RPC);
//     rpc_req->setResult(33);
//     rpc_req->setRCode(rcrpc::RCode::RCODE_OK);
//     conn->send(rpc_req);
// }

// void onTopicResquest(const rcrpc::BaseConnection::ptr& conn,rcrpc::TopicRequest::ptr& msg)
// {
//     std::cout << "收到了Topic请求:";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_topic = rcrpc::MessageFactory::create<rcrpc::TopicResponse>();
//     rpc_topic->setId("222");
//     rpc_topic->setMType(rcrpc::MType::RSP_TOPIC);
//     rpc_topic->setRCode(rcrpc::RCode::RCODE_OK);
//     conn->send(rpc_topic);
// }

// int main()
// {   
//     auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
//     dispatcher->registerHandler<rcrpc::RpcRequest>(rcrpc::MType::REQ_RPC, onRpcResquest); // 注册映射关系
//     dispatcher->registerHandler<rcrpc::TopicRequest>(rcrpc::MType::REQ_TOPIC, onTopicResquest); 

//     auto server = rcrpc::ServerFactory::create(9090);
//     auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
//         std::placeholders::_1, std::placeholders::_2);
//     server->setMessageCallback(message_cb);
//     server->start();
//     return 0; 
// }


// // dispatcher测试
// #include "../../common/dispatcher.hpp"
// // 客户端服务器通信测试
// #include "../../common/message.hpp"
// #include "../../common/net.hpp"

// void onRpcResquest(const rcrpc::BaseConnection::ptr& conn,rcrpc::RpcRequest::ptr& msg)
// {
//     std::cout << "收到了Rpc请求:" << msg->method() << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_req = rcrpc::MessageFactory::create<rcrpc::RpcResponse>();
//     rpc_req->setId("11111");
//     rpc_req->setMType(rcrpc::MType::RSP_RPC);
//     rpc_req->setResult(33);
//     rpc_req->setRCode(rcrpc::RCode::RCODE_OK);
//     conn->send(rpc_req);
// }

// void onTopicResquest(const rcrpc::BaseConnection::ptr& conn,rcrpc::TopicRequest::ptr& msg)
// {
//     std::cout << "收到了Topic请求:";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_topic = rcrpc::MessageFactory::create<rcrpc::TopicResponse>();
//     rpc_topic->setId("222");
//     rpc_topic->setMType(rcrpc::MType::RSP_TOPIC);
//     rpc_topic->setRCode(rcrpc::RCode::RCODE_OK);
//     conn->send(rpc_topic);
// }

// int main()
// {   
//     auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
//     dispatcher->registerHandler<rcrpc::RpcRequest>(rcrpc::MType::REQ_RPC, onRpcResquest); // 注册映射关系
//     dispatcher->registerHandler<rcrpc::TopicRequest>(rcrpc::MType::REQ_TOPIC, onTopicResquest); 

//     auto server = rcrpc::ServerFactory::create(9090);
//     auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
//         std::placeholders::_1, std::placeholders::_2);
//     server->setMessageCallback(message_cb);
//     server->start();
//     return 0; 
// }

// void omMessage(const rcrpc::BaseConnection::ptr& conn,rcrpc::BaseMessage::ptr& msg)
// {
//     std::string body = msg->serialize();
//     std::cout << "recv msg: " << body << std::endl;
//     auto rpc_rep = rcrpc::MessageFactory::create<rcrpc::RpcResponse>();
//     rpc_rep->setId("11111");
//     rpc_rep->setMtype(rcrpc::MType::RSP_RPC);
//     rpc_rep->setRCode(rcrpc::RCode::RECODE_OK);
//     rpc_rep->setResult("33");
//     conn->send(rpc_rep);
// }

// int main()
// {
//     auto server = rcrpc::ServerFactory::create(9090);
//     server->setMessageCallback(omMessage);
//     server->start();
//     return 0; 
// }